package cn.kattomy.shop.product.service.impl;

import java.util.List;

import cn.kattomy.shop.image.vo.Image;
import cn.kattomy.shop.product.dao.ProductDao;
import cn.kattomy.shop.product.service.ProductService;
import cn.kattomy.shop.product.vo.Product;
import cn.kattomy.shop.utils.PageBean;

public class ProductServiceImpl implements ProductService {
	
	private ProductDao productDao;
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	@Override
	public List<Product> getHot() {
		List<Product> hList =  productDao.queryHotProduct();
		return hList;
	}

	@Override
	public List<Product> getNew() {
		List<Product> nList = productDao.queryNewProduct();
		return nList;
	}

	@Override
	public Product findByPid(Integer pid) {
		return productDao.queryByPid(pid);
	}

	@Override
	public PageBean<Product> findByPageCid(Integer cid, int pageNo) {
		int pageSize = 12;
		PageBean<Product> pageBean = new PageBean<>(pageNo, pageSize);
		int totalItemNo = productDao.findCountByCid(cid);
		pageBean.setTotalItemNo(totalItemNo);
		int begin = (pageNo - 1)*pageSize;
		List<Product> list = productDao.findByPageCid(cid, begin, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

	@Override
	public PageBean<Product> findByPageCsid(Integer csid, int pageNo) {
		int pageSize = 12;
		PageBean<Product> pageBean = new PageBean<>(pageNo, pageSize);
		int totalItemNo = productDao.findCountByCsid(csid);
		pageBean.setTotalItemNo(totalItemNo);
		int begin = (pageNo - 1)* pageSize;
		List<Product> list = productDao.findByPageCsid(csid, begin, pageSize);
		pageBean.setList(list);
		
		return pageBean;
	}

	@Override
	public PageBean<Product> findByPageNo(Integer pageNo) {
		int pageSize = 20;
		//设置当前页码以及每页显示的记录数
		PageBean<Product> pageBean = new PageBean<>(pageNo, pageSize);
		//设置总记录数
		int totalItemNo = productDao.findCount();
		pageBean.setTotalItemNo(totalItemNo);
		//设置当前页面显示的记录的 List
		int begin = (pageNo - 1)* pageSize;
		List<Product> list = productDao.findByPageNo(begin, pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public void save(Product product) {
		productDao.save(product);
	}

	@Override
	public void delete(Product product) {
		productDao.delete(product);
	}

	@Override
	public void update(Product product) {
		productDao.update(product);
	}

	@Override
	public double findCount() {
		return productDao.findCount();
	}

	@Override
	public List<Product> findAll() {
		return productDao.findAll();
	}

	@Override
	public void saveImage(Image image) {
		productDao.saveImage(image);
	}

	@Override
	public double findImageCount() {
		return productDao.findImageCount();
	}

	@Override
	public PageBean<Product> searchByKeyword(Integer pageNo, String keyword) {
		return productDao.searchByKeyword(keyword, pageNo);
	}

	@Override
	public void createIndexs() {
		productDao.createIndex();
	}
	
}
